package com.zhuoyue.web.controller.system;

import com.zhuoyue.common.constant.Constants;
import com.zhuoyue.common.constant.UserConstants;
import com.zhuoyue.common.core.controller.BaseController;
import com.zhuoyue.common.core.domain.AjaxResult;
import com.zhuoyue.common.core.domain.entity.SysDictData;
import com.zhuoyue.common.core.domain.entity.SysDictType;
import com.zhuoyue.common.core.page.TableDataInfo;
import com.zhuoyue.common.utils.SecurityUtils;
import com.zhuoyue.common.utils.poi.ExcelUtil;
import com.zhuoyue.system.service.ISysDictDataService;
import com.zhuoyue.system.service.ISysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author zhanghui
 * @Date: 2021/08/05/11:47
 * @Description:
 */

@RestController
@RequestMapping("/system/dict")
public class SysDictController extends BaseController {
    @Autowired
    private ISysDictTypeService dictTypeService;

    @Autowired
    private ISysDictDataService dictDataService;

    /**
     * 查询字典类型列表
     *
     * @param dictType
     * @return
     */
    @GetMapping("/type/list")
    public TableDataInfo typeList(SysDictType dictType) {
        startPage();
        List<SysDictType> list = dictTypeService.selectDictTypeList(dictType);
        return getDataTable(list);
    }

    /**
     * 新增字典类型
     */
    @PostMapping("/type/add")
    public AjaxResult typeAdd(@Validated @RequestBody SysDictType dict) {
        if (Constants.NOT_UNIQUE.equals(dictTypeService.checkDictTypeUnique(dict))) {
            return AjaxResult.error("新增字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        dict.setCreateBy(SecurityUtils.getUsername());
        return toAjax(dictTypeService.insertDictType(dict));
    }

    /**
     * 修改字典类型
     */
    @PutMapping("/type/update")
    public AjaxResult typeUpdate(@Validated @RequestBody SysDictType dict) {
        if (UserConstants.NOT_UNIQUE.equals(dictTypeService.checkDictTypeUnique(dict))) {
            return AjaxResult.error("修改字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        dict.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(dictTypeService.updateDictType(dict));
    }

    /**
     * 删除字典类型
     */
    @DeleteMapping("/type/delete/{dictIds}")
    public AjaxResult typeDelete(@PathVariable Long[] dictIds) {
        return toAjax(dictTypeService.deleteDictTypeByIds(dictIds));
    }

    /**
     * 导出字典类型
     */
    @GetMapping("/type/export")
    public AjaxResult TypeExport(SysDictType sysDictType) {
        List<SysDictType> list = dictTypeService.selectDictTypeList(sysDictType);
        ExcelUtil<SysDictType> util = new ExcelUtil<SysDictType>(SysDictType.class);
        return util.exportExcel(list, "字典类型");
    }

    /**
     * 查询字典数据列表
     *
     * @param dictData
     * @return
     */
    @GetMapping("/data/list")
    public TableDataInfo dataList(SysDictData dictData) {
        startPage();
        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
        return getDataTable(list);
    }

    /**
     * 导出字典数据
     */
    @GetMapping("/data/export")
    public AjaxResult dataExport(SysDictData dictData) {
        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
        ExcelUtil<SysDictData> util = new ExcelUtil<>(SysDictData.class);
        return util.exportExcel(list, "字典数据");
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/data/listByType/{dictType}")
    public AjaxResult dictType(@PathVariable String dictType) {
        return AjaxResult.success(dictTypeService.selectDictDataByType(dictType));
    }

    /**
     * 新增字典类型
     */
    @PostMapping("/data/add")
    public AjaxResult dataAdd(@Validated @RequestBody SysDictData dict) {
        dict.setCreateBy(SecurityUtils.getUsername());
        return toAjax(dictDataService.insertDictData(dict));
    }

    /**
     * 修改保存字典类型
     */
    @PutMapping("/data/update")
    public AjaxResult dataUpdate(@Validated @RequestBody SysDictData dict) {
        dict.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(dictDataService.updateDictData(dict));
    }

    /**
     * 删除字典类型
     */
    @DeleteMapping("/data/delete/{dictCodes}")
    public AjaxResult remove(@PathVariable Long[] dictCodes) {
        return toAjax(dictDataService.deleteDictDataByIds(dictCodes));
    }
}
